import glob
import os
import shutil
import pandas as pd

from 判断是否有广告弹窗 import yes_or_no


def get_data_only(page, down_path, date_start, date_end, date_lastmonth):
    shop_name = '抖音小店-' + page(".index_userName__16Isl").text
    if os.path.exists(f"C:\\抖音退款数据\\{date_lastmonth}\\{shop_name}.xlsx"):
        return
    # page('@data-id=552').click()
    page('@data-id=553').click()
    yes_or_no(page)
    page('更多筛选').click()
    page('.auxo-picker-input auxo-picker-input-active').click()
    if page(f'@title={date_start}', timeout=2) and page(f'@title={date_end}', timeout=2):
        page(f'@title={date_start}').click()
        page(f'@title={date_end}').click()
    else:
        page('.auxo-picker-header-prev-btn').click()
        page(f'@title={date_start}').click()
        page(f'@title={date_end}').click()
    page('确定').click()
    page('.auxo-btn auxo-btn-dashed auxo-btn-sm').click()
    if page('暂无数据', timeout=2):
        print('暂无数据')
        df = pd.DataFrame()
        df.to_excel(f"C:\\抖音退款数据\\{date_lastmonth}\\{shop_name}.xlsx")
    else:
        page('导出售后单').click()
        print('等待售后单导出')
        page.wait.download_begin(timeout=60)
        page.wait.downloads_done(timeout=60)
        print('售后单导出成功')
    page.wait(3)
    # 获取文件夹中所有的.xlsx文件
    xlsx_files = glob.glob(os.path.join(down_path, "*.xlsx"))
    os.makedirs(f'C:\\抖音退款数据\\{date_lastmonth}', exist_ok=True)
    shutil.move(xlsx_files[0], f"C:\\抖音退款数据\\{date_lastmonth}\\{shop_name}.xlsx")
